package com.education.platform.management.controller.user;

import com.education.platform.common.util.BeanUtil;
import com.education.platform.dao.model.AccountFlowModel;
import com.education.platform.dao.model.UserBaseModel;
import com.education.platform.management.service.AccountFlowService;
import com.education.platform.management.service.AccountService;
import com.education.platform.management.service.UserBaseService;
import com.education.platform.management.vo.request.ChargeBeansVO;
import com.education.platform.management.vo.request.GiveBeansQueryVO;
import com.education.platform.management.vo.request.UserBaseQueryVO;
import com.education.platform.management.vo.response.GiveBeansPageInfoVO;
import com.education.platform.management.vo.response.GiveBeansResponseVO;
import com.education.platform.management.vo.response.UserBasePageInfoVO;
import com.education.platform.management.vo.response.UserBaseResponseVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.*;

/**
* describe: app用户
* @author : tomato
* @date: 2020-06-29 10:28:08.729
**/
@RestController
@Api("app用户")
@RequestMapping("userBase")
@Validated
public class UserBaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserBaseController.class);

	@Autowired
    private UserBaseService userBaseService;

	@Autowired
    private AccountService accountService;

	@Autowired
    private AccountFlowService accountFlowService;

    /**
     * 分页查询
     * @param userBaseQueryVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "分页查询app用户 ")
    @PostMapping("getPageList")
    public UserBasePageInfoVO selectPageList(@RequestBody @Valid UserBaseQueryVO userBaseQueryVO) {
        UserBaseModel userBase = BeanUtil.convert(userBaseQueryVO, UserBaseModel.class);
        userBase.setDeleted(false);
        Page<UserBaseResponseVO> page = PageHelper.startPage(userBaseQueryVO.getPageNum(), userBaseQueryVO.getPageSize());
        List<UserBaseModel> userBaseList = userBaseService.getByCondition(userBase);
        UserBasePageInfoVO userBasePageInfo = new UserBasePageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), userBasePageInfo);
        userBasePageInfo.setPageNum(userBaseQueryVO.getPageNum());
        List<UserBaseResponseVO> voList = BeanUtil.convert(userBaseList, UserBaseResponseVO.class);
        if(userBaseQueryVO.getHiddenPhone() == null||userBaseQueryVO.getHiddenPhone()) {
            if (CollectionUtils.isNotEmpty(voList)) {
                voList.parallelStream().forEach(m -> {
                    m.setPhone(m.getPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
                });
            }
        }
        userBasePageInfo.setList(voList);
        return userBasePageInfo;
    }



    /**
     * 后台赠送豆子
     * @param chargeBeansVO
     *
     */
    @ApiOperation(value = "后台赠送豆子 ")
    @PostMapping("giveBeans")
    public void giveBeans(@RequestBody @Valid ChargeBeansVO chargeBeansVO) {
        accountService.chargeBeans(chargeBeansVO.getUserId(),chargeBeansVO.getBeans());
    }


    /**
     * 赠送记录
     * @param queryVO
     *
     */
    @ApiOperation(value = "赠送记录 ")
    @PostMapping("giveBeansRecord")
    public GiveBeansPageInfoVO giveBeansRecord(@RequestBody @Valid GiveBeansQueryVO queryVO) {
        AccountFlowModel query = BeanUtil.convert(queryVO,AccountFlowModel.class);
        Page page= PageHelper.startPage(queryVO.getPageNum(),queryVO.getPageSize());
        List<AccountFlowModel> list = accountFlowService.getGiveBeansRecord(query);
        GiveBeansPageInfoVO res = new GiveBeansPageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), res);
        res.setPageNum(queryVO.getPageNum());
        List<GiveBeansResponseVO> voList = BeanUtil.convert(list, GiveBeansResponseVO.class);
        res.setList(voList);
        return res;
    }

}
